import { defineStore } from 'pinia';
import { userDB, draftDB, initDB } from '../services/database';

// 初始化数据库
initDB();

export const useResumeStore = defineStore('resume', {
  state: () => ({
    // 用户信息
    profile: {
      // basicInfo: {
      //   name: '',
      //   phone: '',
      //   email: '',
      //   gender: '',
      //   status: ''
      // },
      // education: [],
      // workExperience: [],
      // projects: [],
      // skills: [],
      // campusExperience: [],
      // jobObjectives: [],
      // awards: []
    },
    // 草稿列表
    drafts: [],
  }),

  getters: {
    // 获取完整简历信息
    fullResume: (state) => {
      return {
        ...state.profile.basicInfo,
        education: state.profile.education,
        workExperience: state.profile.workExperience,
        projects: state.profile.projects,
        skills: state.profile.skills
      }
    }
  },

  actions: {
    // 加载用户数据
    async loadProfile() {
      this.profile = await userDB.getProfile();
    },

    // 保存用户数据
    async saveProfile(newProfile) {
      await userDB.saveProfile(newProfile);
      this.profile = newProfile;
    },

    // 加载草稿列表
    async loadDrafts() {
      this.drafts = await draftDB.getDrafts();
    },

    // 保存草稿
    async saveDraft(newDraft) {
      await draftDB.saveDraft(newDraft);
      this.drafts.push(newDraft);
    },

    // 删除草稿
    async deleteDraft(draftId) {
      this.drafts = this.drafts.filter(d => d.id !== draftId);
      // 更新数据库
      await draftDB.deleteDraft(draftId);
    },

    // // 更新基本信息
    // updateBasicInfo(info) {
    //   this.profile.basicInfo = { ...this.profile.basicInfo, ...info }
    // },

    // // 更新教育背景
    // updateEducation(edu) {
    //   this.profile.education = { ...this.profile.education, ...edu }
    // },

    // // 添加工作经历
    // addWorkExperience(work) {
    //   this.profile.workExperience.push(work)
    // },

    // // 添加项目经验
    // addProject(project) {
    //   this.profile.projects.push(project)
    // },

    // // 添加技能
    // addSkill(skill) {
    //   this.profile.skills.push(skill)
    // },

    // // 添加校园经历
    // addCampusExperience(experience) {
    //   this.profile.campusExperience.push(experience)
    // },

    // // 添加求职意向
    // addJobObjective(objective) {
    //   this.profile.jobObjectives.push(objective)
    // },

    // // 添加奖项
    // addAward(award) {
    //   this.profile.awards.push(award)
    // },

    // // 更新样式设置
    // updateStyleSettings(settings) {
    //   this.styleSettings = { ...this.styleSettings, ...settings }
    // },

    // // 更新完整简历数据
    // updateResumeData(data) {
    //   if (data.basicInfo) {
    //     this.updateBasicInfo(data.basicInfo)
    //   }
    //   if (data.education) {
    //     this.updateEducation(data.education)
    //   }
    //   if (data.workExperience) {
    //     this.profile.workExperience = data.workExperience
    //   }
    //   if (data.projects) {
    //     this.profile.projects = data.projects
    //   }
    //   if (data.skills) {
    //     this.profile.skills = data.skills
    //   }
    //   if (data.campusExperience) {
    //     this.profile.campusExperience = data.campusExperience
    //   }
    //   if (data.jobObjectives) {
    //     this.profile.jobObjectives = data.jobObjectives
    //   }
    //   if (data.awards) {
    //     this.profile.awards = data.awards
    //   }
    // },

    // 保存所有表单数据
    async saveAllFormData(formData) {
      // for (const [key, value] of Object.entries(formData)) {
      //   if (value) {
      //     switch (key) {
      //       case 'basic':
      //         this.updateBasicInfo(value)
      //         break
      //       case 'education':
      //         this.updateEducation(value)
      //         break
      //       case 'experience':
      //         this.profile.workExperience = value
      //         break
      //       case 'projects':
      //         this.profile.projects = value
      //         break
      //       case 'skills':
      //         this.profile.skills = value
      //         break
      //       case 'campus':
      //         this.profile.campusExperience = value
      //         break
      //       case 'objective':
      //         this.profile.jobObjectives = value
      //         break
      //       case 'awards':
      //         this.profile.awards = value
      //         break
      //     }
      //   }
      // }
      // 序列化Profile对象
      const serializedProfile = JSON.parse(JSON.stringify(formData));
      
      // 保存到Profile
      try {
        await userDB.saveProfile(serializedProfile);
        return true;
      } catch (error) {
        console.error('保存Profile失败:', error);
        return false;
      }
    }
  }
});
